<?php

/**
 * ReceivingController
 * 
 * @author
 * @version 
 */

class Merchant_ReceivingController extends Zend_Controller_Action {
	
	public function preDispatch(){
		$this->tplDirectory = "merchant/receiving/";
	}
	
	/**
	 * List all Receiving Orders
	 */
	public function indexAction() {
		
	}

	public function createAction() {		

		if($this->_request->isPost()){
			$customerService = new Merchant_Service_Customer();
			$customerInfo = $customerService->getLoginInfo()->customer;
			$customer_id = $customerInfo['customer_id'];
			
			$product_ids = $this->_request->product_id;	//产品ID 数组
			$op_quantitys = $this->_request->op_quantity;
			$chkArr = $this->_request->qcChk;
			$chkpriorityArr=$this->_request->priority;
			$objConfig = new Default_Model_Config();
			$congifRes = $objConfig->getByValue('QUANTITY_CONTROL_ID');
			//检查勾选的产品是否有设置QC项
			$povServiceObj = new Merchant_Service_ProductOptionValue();
			if(!empty($chkArr)){
				foreach($chkArr as $k=>$v){
					if(!$povServiceObj->getByProductId($k,$congifRes['config_value'])) unset($chkArr[$k]);
				}
			}
			$description = $this->_request->instructions;	//特别说明
			$reference_no = $this->_request->getParam('reference_no','');
			$receiving_id = $this->_request->getParam('receiving_id','');	//提交的receiving_id，为修改状态时不为空
			
			$receivingArr = $productArr = array();
			$receivingArr['customer_id'] = $customer_id;
			$receivingArr['warehouse_id'] = 1;  	//只有一个仓库，ID为 1
			$receivingArr['order_type'] = 1;		//默认为 1 ，标准
			$receivingArr['description'] = $description;
			$receivingArr['ref_id'] = $reference_no;
			$receivingArr['status'] = 1;
			
			$receivingArr['created_by'] = $customer_id;		//创建人
			$receivingArr['modified_by'] = $customer_id;	//修改人
			$receivingArr['created'] = date('Y-m-d H:i:s');
			$receivingArr['modified'] = date('Y-m-d H:i:s');
			$receivingArr['pickup'] = 0;		//暂时默认为0
			$receivingArr['supplier_id'] = '';		//供应商ID 暂时为空			
			
			foreach ($product_ids as $key => $val){
				$productArr[$val] = $op_quantitys[$key];
			}

			$data =array('ask'=>0,'receiving_code'=>'','msg'=>'');	
			$obj = new Merchant_Service_AsnProccess();	
			if($receiving_id){	//更新
				if(isset($receivingArr['created'])){
					unset($receivingArr['created']);
					unset($receivingArr['created_by']);
				}
				$tmpArr = $obj->update($productArr,$receivingArr,$receiving_id,'receiving_id',$chkArr);
			}else{	// 新建				
				$tmpArr = $obj->create($productArr,$receivingArr, null,$chkArr,$chkpriorityArr);	
			}
			if(count($tmpArr)){
				$data = $tmpArr;
			}
			
			die(json_encode($data));			
		}

		echo Cff::renderTpl($this->tplDirectory."create.tpl","user-layout");
	
	}
	
	public function listAction() {
		
		$status = $this->_request->getParam('status',1);
		$this->view->status = $status;
		
		if($this->_request->isPost()){
			$customerService = new Merchant_Service_Customer();
			$customerInfo = $customerService->getLoginInfo()->customer;
			$customer_id = $customerInfo['customer_id'];
			if(!$customer_id)return false;
			$orderType = Merchant_Service_Receiving::$orderType;
			$cond =array();
			$receiving_code = $this->_request->getParam('receiving_code','');
			$ref_id = $this->_request->getParam('ref_id','');			
			$dateFrom = $this->_request->getParam('dateFrom','');
			$dateTo = $this->_request->getParam('dateTo','');
			$status = $this->_request->getParam('status','');
			
			$this->view->status = $status;
			$receiving_code && $cond['receiving_code'] = $receiving_code;
			$ref_id && $cond['ref_id'] = $ref_id;
			$dateFrom && $cond['dateFrom'] = $dateFrom;
			$dateTo && $cond['dateTo'] = $dateTo;			
			if($status!==''){
				$cond['status'] = $status;
			}
			$cond['customer_id'] = $customer_id; 	//指定用户
			$cond['order_type'] = 1;				//order_type 为 1 ，表示用户预发货

			$page = $this->_request->getParam('page',1);
			$pageSize = $this->_request->getParam('pageSize',20);
			$page = $page?$page:1;
			$data =array('ask'=>0);			
			
			$tmpcond = $cond;
			//delete total
			$tmpcond['status'] = 0;
			$deleteTotal = Merchant_Service_Receiving::getByCond($tmpcond,'count(*)','receiving_id',0,0);
			
			//Awaiting total
			$tmpcond['status'] = 1;
			$awaitingTotal = Merchant_Service_Receiving::getByCond($tmpcond,'count(*)','receiving_id',0,0);
			
			//Receiving total
			$tmpcond['status'] = 2;
			$receivingTotal = Merchant_Service_Receiving::getByCond($tmpcond,'count(*)','receiving_id',0,0);
			
			//received total
			$tmpcond['status'] = 3;
			$receivedTotal = Merchant_Service_Receiving::getByCond($tmpcond,'count(*)','receiving_id',0,0);
			
			//Abnormal total
			$tmpcond['status'] = 4;
			$abnormalTotal = Merchant_Service_Receiving::getByCond($tmpcond,'count(*)','receiving_id',0,0);
			$data['statusTotal'] = array(
							"deleteTotal" => $deleteTotal,
							"awaitingTotal" => $awaitingTotal,
							"receivingTotal" => $receivingTotal,
							"receivedTotal" => $receivedTotal,
							"abnormalTotal" => $abnormalTotal );
							
			$total = Merchant_Service_Receiving::getByCond($cond,'count(*)','receiving_id',0,0);
			$rows = Merchant_Service_Receiving::getByCond($cond,'*','created desc',$pageSize,($page-1)*$pageSize);
			if(!$total){
				die(json_encode($data));
			}
			$serviceObj = new Merchant_Service_Product();			
			foreach ($rows as $key =>$val){
				$products = array();
				$products = Merchant_Service_ReceivingItem::getAllByValue($val['receiving_code'],'receiving_code');
				$totalQty = $received_qty_all = 0;
				foreach ($products as $k2 =>$v2){
					$productInfo = $serviceObj->getProductInfo($v2['product_id']);
					$products[$k2]['SKUNo'] = $productInfo['product_sku'];
					$products[$k2]['SKUName'] = $productInfo['product_title'];
					
					if($v2['remark']===NULL){
						$products[$k2]['remark'] = '';
					}
					$totalQty += $v2['order_qty'];	//创建时产品总数
					$received_qty_all += $v2['received_qty'];	//收货时产品总数
				}
				$rows[$key]['order_type'] = $orderType[$val['order_type']];
				$rows[$key]['products']=$products;
				$rows[$key]['totalQty']=$totalQty;
				$rows[$key]['received_qty'] = $received_qty_all;
			}
			
			$data['ask'] = 1;
			$data['total'] = $total;
			$data['result'] = $rows;			
			die(json_encode($data));
		}
		
		echo Cff::renderTpl($this->tplDirectory."list.tpl","user-layout");	
	}
	
	public function editAction() {
		$receiving_id = $this->_request->getParam('id','');
		$this->view->receiving_id  = $receiving_id ;
		echo Cff::renderTpl($this->tplDirectory."edit.tpl","user-layout");
	}
	
	public function printAsnnoAction(){
		$receiving_code = $this->_request->getParam('asn','');
		$result = Merchant_Service_Receiving::getByValue($receiving_code,'receiving_code');
		
		$this->view->result = $result;
		echo $this->view->render($this->tplDirectory."print.tpl");
	}
	
	public function barcodeAction(){
		Merchant_Service_Barcode::barcode($this->_request->code);
	}
	
	public function receivingProductAction(){
		$receiving_id = $this->_request->getParam('receiving_id','');
		$receiving = Merchant_Service_Receiving::getByValue($receiving_id,'receiving_id');
		$products = Merchant_Service_ReceivingItem::getAllByValue($receiving['receiving_code'],'receiving_code');
		$data =array('ask'=>0);
		$productArr = array();
		$serviceObj = new Merchant_Service_Product();
		
		foreach ($products as $key => $val){
			$productInfo = $serviceObj->getProductInfo($val['product_id']);
			$productInfo['order_qty'] = $val['order_qty'];
			$productInfo['is_qc'] = $val['is_qc'];
			$productInfo['product_status'] = $serviceObj->getStatusText($productInfo['product_status']);
			$productInfo && $productArr[] = $productInfo;
		}
		if(count($productArr)){
			$data['ask'] = 1;
			$data['receiving'] = $receiving;
			$data['result'] = $productArr;
		}
		die(json_encode($data));
	}
	
	public function deleteAction(){
		$receiving_code = $this->_request->getParam('id','');
		$data = array('ask'=>0);
		$row = array('status'=>0);
		//删除 receiving 更改status 为 0
		$receivingObj = new Merchant_Service_Receiving();
		$res = $receivingObj->update($row,$receiving_code,'receiving_code');

		if($res){
			$data['ask'] = 1;
		}
		die(json_encode($data));
	}
	
	/**
	 * 	检查 reference_no 唯一性，不存在返回true，
	 *  即当前reference_no 可以使用
	 */
	public function checkRefnoAction(){
		$reference_no = trim($this->_request->getParam('reference_no',''));
		if($reference_no != ''){
			$obj = new Merchant_Service_AsnProccess();
			$res = $obj->checkReferenceNo($reference_no);
			die(json_encode($res));
		}
	}
	
	public function isqcCheckAction(){
		$isChecked = $this->_request->getParam('isChecked','');
		$pid = $this->_request->getParam('pid','');
		//为true时判断是否有设置qc项  为false时检查该产品是否有设置is_qc
		$objConfig = new Default_Model_Config();
		$congifRes = $objConfig->getByValue('QUANTITY_CONTROL_ID');
		$return = array();
		if($isChecked == 'true'){
			$povServiceObj = new Merchant_Service_ProductOptionValue();
			if($povServiceObj->getByProductId($pid,$congifRes['config_value'])) $return['checkedTrue']=1; 
			else $return['checkedTrue']=0;
		}else{
			$proServiceObj = new Merchant_Service_Product();
			$res = $proServiceObj->getProductInfo($pid);
			if($res['product_is_qc']==1) $return['checkedFalse']=1; 
			else $return['checkedFalse']=0;
		}
		die(json_encode($return));
	}

}
